package com.sample;


import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class RSAUtil {

    //解密
    public static String encrypt(String dataString, String publicDecodeKeyString) {
        try {
            PublicKey publicKey = RSAUtil.loadPublicKey(publicDecodeKeyString);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,publicKey);

            byte[] bytes = dataString.getBytes("UTF-8");
            byte[] b = cipher.doFinal(bytes);
            return Base64.getEncoder().encodeToString(b);

        }catch (Exception e){
            e.printStackTrace();
        }

        return "";

    }

    //解密
    public static String decrypt(String dataString, String privateDecodeKeyString) {
        try {
            PrivateKey privateKey = RSAUtil.loadPrivateKey(privateDecodeKeyString);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.DECRYPT_MODE,privateKey);

            byte[] bytes = Base64.getDecoder().decode(dataString);
            byte[] b = cipher.doFinal(bytes);
            return new String(b);

        }catch (Exception e){
            e.printStackTrace();
        }

        return "";

    }

    /**
     * 签名字符串
     *
     * @param text       需要签名的字符串
     * @param privateKey 私钥(BASE64编码)
     * @return 签名结果(BASE64编码)
     */
    public static String sign(String text, String privateKey) {

        //byte[] keyBytes = Base64.getDecoder().decode(privateKey);
        //PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
        try {
            //KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            //PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec);
            PrivateKey privateK = loadPrivateKey(privateKey);
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initSign(privateK);
            signature.update(text.getBytes());
            byte[] result = signature.sign();
            return Base64.getEncoder().encodeToString(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //验签
    public static boolean verify(String textString,String signStr, String publicKeyString){
        PublicKey key = RSAUtil.loadPublicKey(publicKeyString);
        try {
            Signature signature = Signature.getInstance("SHA256WithRSA");
            signature.initVerify(key);
            signature.update(textString.getBytes());

            boolean success = signature.verify(Base64.getDecoder().decode(signStr));
            System.out.println("验证成功？--"+(success?"YES":"NO"));
            return success;
        }catch (Exception e){
            e.printStackTrace();
        }
        return false;
    }

    //加载公钥
    public static RSAPublicKey loadPublicKey(String publicKeyString){
        try {
            byte[] buffer = Base64.getDecoder().decode(publicKeyString);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer);
            return (RSAPublicKey)keyFactory.generatePublic(keySpec);
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }


    //加载私钥
    public static RSAPrivateKey loadPrivateKey(String priKeyString){
        try {
            PKCS8EncodedKeySpec priPK = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(priKeyString));
            KeyFactory factory = KeyFactory.getInstance("RSA");
            PrivateKey privateKey = factory.generatePrivate(priPK);

            return (RSAPrivateKey) privateKey;
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }



}
